<?php
/**
 * 企业
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/16  17:00
 */

namespace app\pc\Controller;


class Company extends Base
{
    /*
    *   构造函数
    */
    function __construct(){
        parent::__construct();
    }

    //首页
    public function index(){

        $pageParam =  ['query'=>[]];
        $company_result = [];
        $page = '';
        $company_count = 0;

        $where = [];
        $uid = $this->info_user['id'];

        $company_name = input('company_name');
        $order = input('order');

        if($company_name != '')
        {
            $where['name'] = array('like','%'.$company_name.'%');
            $pageParam['query']['company_name'] = $company_name;
            $this->assign('company_name', $company_name);
            //热搜
            $hot_search = cache("hot_search"); 
            $hot_search = json_decode($hot_search,true);
            $hot_search[] = $company_name;
            cache("hot_search", json_encode($hot_search), 864000);
        }
        $province = input('province');

        if($province != "")
        {
            $where['province'] = $province;
        }

        $city = input('city');
        if($city != "")
        {
            $where['city'] = $city;
        }

        $area = input('area');
        if($area != "")
        {
            $where['area'] = $area;
        }

        $company_result = db('xls_company')
            ->where($where)
            ->paginate(8, false, $pageParam)
            ->each(function($item,$key){
                $are_id = [$item['province'],$item['city'],$item['area']];
                $where['id'] = array('in',$are_id);
                $area_result = db('area')->where($where)->field('name')->order('id asc')->select();
                $item['province'] = isset($area_result[0]['name'])?$area_result[0]['name']:'';
                $item['city'] = isset($area_result[1]['name'])?$area_result[1]['name']:'';
                $item['area'] = isset($area_result[2]['name'])?$area_result[2]['name']:'';
                $item['path_tem'] = $item['province'].$item['city'].$item['area'];

                if(!empty($this->info_user['id'])){
                    $attention = db('attention')->where(array('uid'=>$this->info_user['id'],'company_id'=>$item['id']))->find();
                    if(!empty($attention)){
                        $item['is_attention'] = $attention['status'];
                    }else{
                        $item['is_attention'] = 0;
                    } 
                }else{
                    $item['is_attention'] = 0;
                }

                return $item;
            });

        $company_count = $company_result->total();
        $page = $company_result->render();

        //热搜
        $hot_new_array = array();
        $hot_search = cache("hot_search");
        $hot_search = json_decode($hot_search,true);
        //将重复的元素值筛选出来
        if($hot_search) {
            $hot_array = array_count_values($hot_search);
            $i = 0;
            foreach ($hot_array as $key => $value) {
                $hot_new_array[$i]['search_name'] = $key;
                $hot_new_array[$i]['search_count'] = $value;
                $i++;
            }
            array_multisort(array_column($hot_new_array, 'search_count'), SORT_DESC, $hot_new_array);
        }
        $hot_new_array = array_slice($hot_new_array,0,5);

        $this->assign('hot_search', $hot_new_array);
        $this->assign('company_result',$company_result);
        $this->assign('company_count',$company_count);
        $this->assign('page',$page);
        return $this->fetch('company/list');
    }

    //关注
    public function follow(){
        $company_id = input('company_id');
        $status = input('status');
        $uid = $this->info_user['id'];

        if(request()->isAjax()) {
            if(empty($uid)){
                $this->error('请先登录!');
            }

            if($status == '1' || $status == '0'){
                $result = member_right_num($uid, 'enterprise_follow_num');
     
                if(isset($result['bool'])){
                    if(!$result['bool']){
                        $this->error('你没有关注权利或者你关注次数已用完');
                    }
                }
            }

            $result_attention = db('attention')->where(array('company_id'=>$company_id,'uid'=>$uid))->find();

            if(empty($result_attention)){
                $data['uid'] = $uid;
                $data['company_id'] = $company_id;
                $data['status'] = 1;
                db('attention')->insert($data);
            }else{
                db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->update(array('status'=>$status));
            }
        }

    }

    //查询是否有权利
    public function company_condition_info(){
        if(!$this->info_user['id'])$this->error('请先登录');

        $uid = $this->info_user['id'];

        $company_id = input('company_id');

        $result = member_right_num($uid,'project','',false);

        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $this->success('查询成功', url('Company/company_info',array('company_id'=>$company_id)));
    }


    //企业详情
    public function company_info(){
        $company_id = input('company_id');
        $uid = $this->info_user['id'];

        $result = member_right_num($uid,'project','',false);
        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $project = db('xls_project')->alias('p')->join('project_type pt','pt.id=p.type')->where(array('p.company_id'=>$company_id))->field('p.*,pt.type_name')->select();
        $project_result = [];
        foreach($project as $k=>$v){
            $project_result[$v['type']]['type_name'] = $v['type_name'];
            $project_result[$v['type']]['type_array'][]= $v;
        }

        $company_result = db('xls_company')->where(array('id'=>$company_id))->field('name')->find();

        $attention_result = db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->field('status')->find();

        $attention = db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->find();
        if(!empty($attention)){
            $is_attention = $attention['status'];
        }else{
            $is_attention = 0;
        } 

        //更新查询次数
        member_right_num($uid,'project');
        $this->assign('company_name',$company_result['name']);
        $this->assign('project_result',$project_result);
        $this->assign('is_attention',$is_attention);
        $this->assign('attention_result',$attention_result);
        $this->assign('company_id',$company_id);
        return $this->fetch('company/index');
    }  
}